package com.chookss.tiku

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.BaseAdapter
import android.widget.TextView
import androidx.lifecycle.ViewModelProviders
import com.chookss.R
import com.chookss.tiku.entity.Subject
import com.chookss.tiku.util.TiKuUtils
import com.johnrambo.ktea.common.createLoadingDialog
import com.johnrambo.ktea.ktExtends.observe
import com.lvgroup.hospital.base.BaseAct
import errorToast
import kotlinx.android.synthetic.main.activity_search_subject_result.*


/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/1/5 11:08
 * Note:
 */
class SearchSubjectResultActivity : BaseAct() {
    protected val viewModel by lazy {
        ViewModelProviders.of(this).get(SearchSubjectViewModel::class.java).apply {
            keyword = intent?.getStringExtra("keyword")
        }
    }

    protected val loadingProgress by lazy {
        createLoadingDialog(this) {}
    }

    override fun initData() {
    }
    private var lpFlag:String = "" //0禁止1正常
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_search_subject_result)
        initState()
        //禁止截屏
        if ("0".equals(lpFlag)) {
            window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
        }
        ivLeft.setOnClickListener { onBackPressed() }
        tvTitle.text = intent?.getStringExtra("keyword")
        loadSubjectList()
        listView.setOnItemClickListener { parent, view, position, id ->
            var intent = Intent(this, SearchSubjectDetailActivity::class.java)
            var subject = parent.adapter.getItem(position) as Subject
            intent.putExtra("subject", subject)
            startActivity(intent)
        }
    }

    protected fun loadSubjectList() {
        loadingProgress.show()
        viewModel.getSubjectList().observe(this, {
            loadingProgress.dismiss()
            if (it.error != null) {
                errorToast(it.error.message ?: "")
            } else if (it.data != null) {
                if(it.data.size>0){
                    listView.visibility=View.VISIBLE;
                    llEmpty.visibility=View.GONE;
                    listView.adapter = MyAdapter(it.data, viewModel.keyword?:"")
                }else{
                    listView.visibility=View.GONE;
                    llEmpty.visibility=View.VISIBLE;
                }

            } else {
                errorToast("没有题目")
            }
        })
    }

    class MyAdapter(var subjects: List<Subject>?, var keyword:String) : BaseAdapter(){
        override fun getCount() = subjects?.size?:0

        override fun getItem(position: Int) = subjects!![position]

        override fun getItemId(position: Int) = 0L

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            var view = convertView
            if (view == null) {
                view = LayoutInflater.from(parent!!.context).inflate(R.layout.item_search_subject, null)
            }
            var subject = getItem(position)
            view!!.findViewById<TextView>(R.id.tvContent).text = createContentText(parent!!.context,subject.subjectContent, keyword)
            view!!.findViewById<TextView>(R.id.tvTip).text = TiKuUtils.subjectTypeName(subject?.subjectTypeCode?:"")
            return view!!
        }

        fun createContentText(context: Context ,text: String, keyword:String): SpannableStringBuilder? {
            val builder = SpannableStringBuilder(text)
            var startIndex = 0
            while (startIndex >= 0) {
                startIndex = text.indexOf(keyword, startIndex)
                if (startIndex >= 0) {
                    builder.setSpan(ForegroundColorSpan(context.resources?.getColor(R.color.main_color)!!), startIndex, startIndex+keyword.length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
                } else {
                    break
                }
                startIndex += keyword.length
            }
            return builder
        }


    }
}